<?php

namespace App\Repository;

use App\Common\Libs\ArrayHelper;
use App\Consts\GlobalConst;
use App\Consts\GlobalTopicConst;
use App\Models\IndexTopic;
use App\Models\Select\TopicSelect;
use App\Models\SysTargetTopic;
use App\Models\SysTopic;
use App\Models\SysTopicTarget;

class TopicRepository extends BaseRepository
{
    public function getTopicList($searchParams)
    {
        $provider = IndexTopic::getInstance()
            ->where('is_deleted', GlobalConst::IS_NOT_DELETE);

        if (!empty($searchParams['keywords_topic'])) {
            $provider->where('title', 'like', '%' . $searchParams['keywords_topic'] . '%');
        }

        if (array_key_exists('type', $searchParams)) {
            $provider->where('type', $searchParams['type']);
        }
        if (array_key_exists('is_hot', $searchParams)) {
            $provider->where('is_hot', $searchParams['is_hot']);
        }
        if (array_key_exists('is_recommend', $searchParams)) {
            $provider->where('is_recommend', $searchParams['is_recommend']);
        }
        if (array_key_exists('ignore', $searchParams)) {
            $provider->whereNotIn('topic_id', (array)$searchParams['ignore']);
        }
        $provider->orderByDesc('created_at');

        return $this->setPagination($provider);
    }

    /**
     * 根据分表数找到一组话题
     * @param $tableCount
     * @param $topicIdArr
     * @param string $selectScene
     * @return SysTopic[]|\Illuminate\Database\Eloquent\Builder[]|\Illuminate\Database\Eloquent\Collection|\Illuminate\Database\Eloquent\Model[]
     */
    public function getTopicByTableCount($tableCount, $topicIdArr, $selectScene = TopicSelect::SELF_LIST)
    {
        return SysTopic::setTableNameByTableCount($tableCount)
            ->select(TopicSelect::getSelects($selectScene))
            ->whereIn('topic_id', (array)$topicIdArr)
            ->get();
    }

    public function getTopicTargetsList($searchParams)
    {
        $provider = SysTopicTarget::getInstance($searchParams['topic_id'])
            ->where([
                'topic_id' => $searchParams['topic_id'],
                'is_deleted' => GlobalConst::IS_NOT_DELETE,
            ]);

        if (array_key_exists('ignore_target', $searchParams)) {
            $provider->whereNotIn('target_id', (array)$searchParams['ignore_target']);
        }

        if (array_key_exists('last_id', $searchParams)) {
            $model = SysTopicTarget::getInstance($searchParams['topic_id'])
                ->where([
                    'topic_id' => $searchParams['topic_id'],
                    'target_id' => $searchParams['last_id']
                ])
                ->first();
            if ($model) {
                $provider->where('created_at', '<', $model->created_at);
            }
        }

        $provider->orderByDesc('created_at');

        return $this->setPagination($provider);
    }

    public function getTargetTopics($targetId)
    {
        $result = [];
        for ($tableCount = 0; $tableCount < GlobalConst::DATABASE_TABLE_NUM; $tableCount++) {
            $models = SysTargetTopic::setTableNameByTableCount($tableCount)
                ->whereIn('target_id', (array)$targetId)
                ->where('is_deleted', GlobalConst::IS_NOT_DELETE)
                ->get();
            $result = ArrayHelper::merge($result, $models);
        }
        return $result;
    }

    /**
     * 获取话题的置顶内容
     * @param $topicId
     * @return SysTopicTarget[]
     */
    public function getTopicTopTargets($topicId)
    {
        return SysTopicTarget::getInstance($topicId)->where([
            'topic_id' => $topicId,
            'is_deleted' => GlobalConst::IS_NOT_DELETE,
            'is_top' => GlobalTopicConst::TARGET_IS_TOP,
        ])->get();
    }

    /**
     * 获取话题的热门内容
     *   注意：不包括置顶内容
     * @param $topicId
     * @return SysTopicTarget[]
     */
    public function getTopicHotTargets($topicId, $ignoreTargetIdArr)
    {
        $query = SysTopicTarget::getInstance($topicId)
            ->where([
                'topic_id' => $topicId,
                'is_deleted' => GlobalConst::IS_NOT_DELETE,
                'is_hot' => GlobalTopicConst::IS_HOT,
            ]);

        if ($ignoreTargetIdArr) {
            $query->whereNotIn('target_id', $ignoreTargetIdArr);
        }

        return $query->get();
    }

    public function getTopicById($topicId, $selectScene = TopicSelect::BASE_INFO)
    {
        return SysTopic::getInstance($topicId)
            ->select(TopicSelect::getSelects($selectScene))
            ->where('topic_id', $topicId)
            ->first();
    }
}
